package cn.tedu.myback.controller;


import cn.tedu.myback.common.enums.UserTypeEnum;
import cn.tedu.myback.common.exception.BusinessException;
import cn.tedu.myback.common.util.FileUtil;
import cn.tedu.myback.converter.UserConverter;
import cn.tedu.myback.dao.UserMapper;
import cn.tedu.myback.pojo.param.UserParam;
import cn.tedu.myback.pojo.po.User;
import cn.tedu.myback.pojo.vo.UserVo;
import cn.tedu.myback.response.AppCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;

@Api(value = "user", tags = "用户")
@Slf4j
@Validated
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserConverter userConverter;

    @Value("${imagePath}")
    private String imagePath;

    @Value("${url}")
    private String url;

    @ApiOperation("用户真实注册")
    @PostMapping("save")
    public void save(@Valid @RequestBody UserParam userParam) {
        //方式一：
//        User user = new User();
//        BeanUtils.copyProperties(userParam,user);

        //不建议直接这么写
//        user.setUserName(userParam.getUserName());
//        user.setNickName(userParam.getNickName());

        //方式二：
        User user = userConverter.convertParamToModel(userParam);
        user.setCreateTime(new Date());
        userMapper.insert(user);
//        return new ResultVo(StatusCode.SUCCESS.getCode(),StatusCode.SUCCESS.getMsg(),"数据");
    }

    @ApiOperation("用户真实注册2")
    @PostMapping("saveUser")
    public void saveUser(@Validated UserParam userParam) {
        User user = userConverter.convertParamToModel(userParam);
        user.setCreateTime(new Date());
        userMapper.insert(user);
    }

    @ApiOperation("用户详情")
    @GetMapping("detail")
    public UserVo getUserInfoById(Long id) { User user = userMapper.selectByPrimaryKey(id);
        if (user == null) {
            throw new BusinessException(AppCode.USER_ERROR);
        }
        return userConverter.convertModelToVo(user);
    }


    @ApiOperation("模拟用户注册")
    @PostMapping("register")
    public void insertUserInfo() {
        log.info("进入 insertUserInfo 方法");
        //测试初始化数据
        User user = new User();
        user.setPassword("11");
        user.setCreateTime(new Date());
        user.setImgUrl("a.jpg");
        user.setUserName("测试");
        user.setNickName("nickName");
        user.setIsAdmin(UserTypeEnum.USER.getValue());
        userMapper.insert(user);
    }

    @PostMapping("fileUpload")
    @ApiOperation("文件上传")
    public String fileUpload(MultipartFile file) {
        //http://dev.img.p.yufeiworld.com/hongbei/a.jpg
        String urlPath = "";
        try {
            String fileName = FileUtil.makeTempFile(file, imagePath);
            urlPath = url + fileName;
        } catch (Exception e) {
            throw new BusinessException(AppCode.FILE_UPLOAD_FAIL);
        }
        return urlPath;
    }

}
